﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Sample1Person.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the Sample1Person type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Samples.Commands
{
    using System.ComponentModel.DataAnnotations;
    using ComponentModel;
    using ComponentModel.Modelling;

    public class Sample1Person : ModelBase
    {
        #region Fields

        private string _surname;
        private string _firstname;
        private double _salary;

        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Sample1Person"/> class.
        /// </summary>
        /// <param name="firstname">The firstname.</param>
        /// <param name="surname">The surname.</param>
        public Sample1Person(string firstname, string surname)
        {
            _surname = surname;
            _firstname = firstname;
        }

        /// <summary>
        /// Gets or sets the firstname.
        /// </summary>
        /// <value>The firstname.</value>
        /// <remarks>
        /// The <see cref="TrimAttribute"/> indicates ensures that the value is trimmed before being applied.
        /// </remarks>
        [Trim, Required]
        public string Firstname
        {
            get
            {
                return _firstname;
            }
            protected set { SetProperty(ref _firstname, value, "Firstname"); }
        }

        /// <summary>
        /// Gets or sets the surname.
        /// </summary>
        /// <value>The surname.</value>
        /// <remarks>
        /// The <see cref="TrimAttribute"/> indicates ensures that the value is trimmed before being applied.
        /// </remarks>
        [Trim, Required]
        public string Surname
        {
            get
            {
                return _surname;
            }
            protected set { SetProperty(ref _surname, value, "Surname"); }
        }

        /// <summary>
        /// Gets or sets the salary.
        /// </summary>
        /// <value>The salary.</value>
        public double Salary
        {
            get { return _salary; }
            protected set { SetProperty(ref _salary, value, "Salary"); }
        }

        /// <summary>
        /// Updates the salary.
        /// </summary>
        /// <param name="value">The value.</param>
        public void UpdateSalary([Range(10000, 100000)] double value)
        {
            Salary = value;
        }

        public bool CanUpdateSalary(double value)
        {
            return value != Salary;
        }

        /// <summary>
        /// Updates the person
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        [Display(Name = "Update Name", Description = "Updates the name.")]
        public void UpdateName(string firstName, string surname)
        {
            Firstname = firstName;
            Surname = surname;
        }

        /// <summary>
        /// Determines whether this instance can be updated with the specified values.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="surname">The surname.</param>
        /// <returns>
        /// <c>true</c> if this instance can be updated with the specified values; otherwise, <c>false</c>.
        /// </returns>
        public bool CanUpdateName(string firstName, string surname)
        {
            firstName = (firstName ?? string.Empty).Trim();
            surname = (surname ?? string.Empty).Trim();
            return (firstName.Length > 0  && surname.Length > 0
                && (_firstname != firstName || _surname != surname));
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            var result = (Firstname ?? string.Empty).Trim();
            if (result .Length > 0)
            {
                var surname = (Surname?? string.Empty).Trim();
                if (surname.Length != 0)
                    result  += " " + surname;
            }

            return result;
        }
    }
}
